package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        /**
         * 1. 保存套餐数据到套餐表setmeal
         * 2. 将setmealDTO拷贝到setmeal对象中
         * 3.将setmeal插入到表中（注意：需要插入后的返回值）
         * 4.从SetmeatDTO中获取setmealDishes集合
         * 5.遍历setmealDishes集合，设置setmealDishes的id
         * 6.批量保存setmealDishes到setmeal_dish表
         *
         */
       Setmeal setmeal = new Setmeal();
       BeanUtils.copyProperties(setmealDTO,setmeal);

       setmealMapper.insert(setmeal);
       log.info("setmeal:{}",setmeal);
       setmealDTO.getSetmealDishes().forEach(setmealDish -> {
           setmealDish.setSetmealId(setmeal.getId());
       });

       setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用PageHelper插件自动进行分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Long[] ids) {
        /**
         * 1.遍历数组获取id
         * 2.如果当前套餐在启售中，不能删除
         * 3.删除套餐时需要将套餐表中的套餐数据删除，同时删除套餐表中的套餐菜品关系表数据
         */
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.delete(id);
            setmealDishMapper.deleteByDishID(id);
        }

    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        /**
         * 1.根据id在setmeal中查询套餐数据
         * 2.根据id在setmeal_dish中查询套餐菜品关系数据
         * 3.将setmeal和setmeal_dish数据封装到SetmealVO中并返回
         */
        Setmeal setmeal = setmealMapper.findById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.findByDishId(id);
        SetmealVO setmealVO = new SetmealVO();
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealVO);
        //封装
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐表数据
        //2.删除套餐表中的套餐菜品关系数据
        //3.批量插入套餐菜品关系数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);
        setmealDishMapper.deleteByDishID(setmealDTO.getId());
        //批量插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dishFlavor -> dishFlavor.setSetmealId(setmealDTO.getId()));
        if (setmealDishes != null && setmealDishes.size()>0){
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //1.查询套餐是否包含启售中的菜品
        //2.套餐状态进行切换
        Setmeal setmeal = new Setmeal();
        if (status == StatusConstant.DISABLE){
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.update(setmeal);
        }else if (status == StatusConstant.ENABLE){
            List<Long> dishId = setmealDishMapper.getDishId(id);
            for (Long id1 : dishId) {
                int dishStatus = dishMapper.getByDishStatus(id1);
                if (dishStatus == 0){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.update(setmeal);
        }
    }
}
